package api

import (
	"blockchain_data/config"
	"blockchain_data/handler/okex/response"
	"blockchain_data/utils"
	"time"
)
func GetDefaultconfig() *Config {
	var apiconfig Config

	// flt. 201812. For swap test env.
	apiconfig.APIHost = config.OkexApiHost
	apiconfig.WSHost = config.OkexWsPublicHost
	apiconfig.ApiKey = ""
	apiconfig.SecretKey = ""
	apiconfig.Passphrase = ""


	apiconfig.TimeoutSecond = config.OkexTimeoutSecond
	apiconfig.IsPrint = false
	apiconfig.I18n = SIMPLIFIED_CHINESE

	

	return &apiconfig
}

func InitAPI() *Client {
	client := NewClient(*GetDefaultconfig())
	return client
}

func (client *Client)GetFunding(optionalParams map[string]string) response.FundingRate {
	fundingRate := response.FundingRate{}
	fundingRateData := []response.FundingRateData{}
	baseUri := SWAP_FUNDING_RATE
	uri := baseUri
	if optionalParams != nil {
		uri = utils.BuildParams(baseUri, optionalParams)
	}
	rep := client.Request(GET, uri, nil)

	if rep.Code == 400{
		fundingRate.Code = 400
	}else {
		fundingRate.Code = 200
		for rep.Header["Ok-Before"] != nil {
			before := rep.Header["Ok-Before"][0]
			optionalParams["before"] = before
			utils.JsonBytes2Struct([]byte(rep.Data), &fundingRate)
			fundingRateData = append(fundingRateData, fundingRate.Data...)
			uri = utils.BuildParams(baseUri, optionalParams)
			rep = client.Request(GET, uri, nil)
			time.Sleep(1 *time.Second)
		}
		fundingRate.Data = fundingRateData
	}
	return fundingRate

}